<!--
 * @Author: MarsLay
 * @Date: 2021-12-23 09:39:56
 * @LastEditTime: 2024-02-21 11:09:56
 * @LastEditors: MarsLay 331412466@qq.com
 * @Description: 自定义搜索组件
 * @FilePath: \cloud-space\components\search\search.vue
-->

<template>
  <view class="search" @click="open">
    <view class="inner" :class="param.confirm ? 'screen' : ''">
      <image class="icon" src="../../static/img/icon/search-icon.png" mode="aspectFit"></image>
      <input class="input" :disabled="disabled" :placeholder="param.placeholder" placeholder-class="searchPla"
        confirm-type="search" v-model="inputValue" @confirm="doSearch" />
      <image class="clear" src="../../static/img/icon/close-s.png" mode="aspectFit" v-if="clear" @click="cancel"></image>
      <view v-if="param.confirm" class="button" @click="search">搜索</view>
    </view>
    <slot></slot>
  </view>
</template>

<script>
export default {
  name: "search",
  components: {},
  props: {
    param: {
      type: Object,
      default: "",
    },
    screen: {
      type: Object,
      default: () => ({}),
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      inputValue: this.screen[this.param.match],
      clear: false,
    };
  },
  computed: {},
  watch: {
    inputValue(v) {
      this.clear = v != "";

      if (v == "") {
        this.$set(this.screen, this.param.match, v);
      }

      if (!this.param.confirm) {
        this.screen[this.param.match] = v;
      }
      this.$emit('update', this.screen[this.param.match]);
    },
  },
  onLoad(d) { },
  onShow() { },
  methods: {
    cancel() {
      this.clear = false;
      this.inputValue = "";
      this.screen[this.param.match] = "";
      this.$emit('search', this.screen);
    },
    search() {
      if (this.inputValue == "") {
        this.$util.tip({ title: `${this.param.placeholder}` });
        return;
      }
      this.$set(this.screen, this.param.match, this.inputValue);
      this.$emit('search', this.screen);
    },
    open() {
      if (!this.disabled) {
        return
      }
      this.$emit('open')
    },
    doSearch() {
      this.$emit('confirm', true);
    }
  },
};
</script>
<style lang='less' scoped>
.search {
  width: 100%;
  height: 80upx;
}

.inner {
  .flex();
  position: relative;
  width: 100%;
  height: 100%;
}

.button {
  z-index: 10;
  position: absolute;
  top: 50%;
  right: 30upx;
  transform: translate(0, -50%);
  font-size: 32upx;
  color: #00B578;
  line-height: 40upx;
}

.search .icon {
  position: absolute;
  transform: translate(0, -50%);
  top: 50%;
  left: 24upx;
  width: 32upx;
  height: 32upx;
}

.input {
  padding: 0 120upx 0 62upx;
  width: 100%;
  height: 100%;
  font-size: 32upx;
  color: #333333;
  border-radius: 16upx;
  background: #ffffff;
  box-sizing: border-box;
}

.searchPla {
  color: #BBBBBB;
  font-size: 32upx;
}

.clear {
  z-index: 10;
  position: absolute;
  top: 50%;
  right: 120upx;
  transform: translate(0, -50%);
  width: 28upx;
  height: 28upx;
}

/deep/ .filterFull {
  padding-left: 20upx;
}
</style>